package com.softeem.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.softeem.domain.ShoppingCar;
import com.softeem.domain.Travle;
import com.softeem.service.IShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/shoppingCar")
public class ShoppingCarController {

    @Autowired
    IShoppingCarService service;
    @Autowired
    StringRedisTemplate redisTemplate;



    @RequestMapping("/addShopping")
    public String addShoppingCar(
                                 @CookieValue("uId")String uId,
                                 @ModelAttribute("traId")Integer traId, Model model) throws Exception {
        Map map = new HashMap();

        Travle travle = new Travle();
        Travle traList = travle.selectById(traId);



        map.put("traId",traId);
        //map.put("uId",uId);
        map.put("tName",traList.getTraName());
        map.put("tTitle",traList.getTraTitle());
        map.put("tPrice",traList.getTraPrice());
        map.put("tIntraduce",traList.getTraIntraduce());

        List<ShoppingCar> carList = service.add(uId, map);
        model.addAttribute("carList",carList);

        return "/likeList";
     }

    @RequestMapping("/deleteShoppingCar")
    public String deleteShoppingCar(@CookieValue("uId")String uId,
                                    @ModelAttribute("traId")String traId,
                                    Model model) throws JsonProcessingException {


        redisTemplate.opsForHash().delete(uId,traId);


        //重新加载redis里的数据
        List<ShoppingCar> carList = new ArrayList<>();
        //获取数据加入进list中
        ObjectMapper objectMapper = new ObjectMapper();
        for (int i = 1;i<redisTemplate.opsForHash().keys(uId).size()+1;i++){
            Object o = redisTemplate.opsForHash().get(uId, String.valueOf(i));
            ShoppingCar shoppingCar = objectMapper.readValue(o.toString(), ShoppingCar.class);
            carList.add(shoppingCar);
        }

        model.addAttribute("carList",carList);

        return "/likeList";
     }


     @RequestMapping("/pay")
     public String pay(@CookieValue("uId")String uId,
                       @ModelAttribute("traId")String traId,
                       Model model) throws JsonProcessingException {

        //加载需支付的redis数据
         List<ShoppingCar> carList = new ArrayList<>();
         ObjectMapper objectMapper = new ObjectMapper();
         Object o = redisTemplate.opsForHash().get(uId, traId);
         ShoppingCar shoppingCar = objectMapper.readValue(o.toString(), ShoppingCar.class);
         carList.add(shoppingCar);
         model.addAttribute("carList",carList);
         return "/pay";
      }


      @RequestMapping("/paySuccess")
      public String paySuccess(@CookieValue("uId")String uId,
                               @ModelAttribute("traId")String traId,
                               Model model) throws JsonProcessingException {

          //redisTemplate.opsForHash().delete(uId,traId);

          redisTemplate.opsForHash().delete(uId,traId);
          //重新加载redis里的数据
          List<ShoppingCar> carList = new ArrayList<>();
          //获取数据加入进list中
          ObjectMapper objectMapper = new ObjectMapper();
          for (int i = 1;i<redisTemplate.opsForHash().keys(uId).size()+1;i++){
              Object o = redisTemplate.opsForHash().get(uId, String.valueOf(i));
              ShoppingCar shoppingCar = objectMapper.readValue(o.toString(), ShoppingCar.class);
              carList.add(shoppingCar);
          }

          model.addAttribute("carList",carList);

          return "/paysuccess";
       }

       @RequestMapping("/selectCar")
       public String selectCar(@CookieValue("uId")String uId,
                         @ModelAttribute("traId")String traId,
                         Model model) throws JsonProcessingException {

           //加载需支付的redis数据
           List<ShoppingCar> carList = new ArrayList<>();
           ObjectMapper objectMapper = new ObjectMapper();
           for (int i = 1;i<redisTemplate.opsForHash().keys(uId).size()+1;i++){
               Object o = redisTemplate.opsForHash().get(uId, String.valueOf(i));
               ShoppingCar shoppingCar = objectMapper.readValue(o.toString(), ShoppingCar.class);
               carList.add(shoppingCar);
           }
           model.addAttribute("carList",carList);
           return "/likelist";
       }



}
